package com.yc.ssq.servlet;

import com.google.gson.Gson;
import com.yc.ssq.bean.Ssq_lottery;
import com.yc.ssq.mapper.SsqMapper;
import com.yc.ssq.util.MyBatisHelper;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//@WebServlet(name = "QueryServlet", value = "/query.do")
@WebServlet("/QueryServlet/*")
public class QueryServlet extends BaseServlet {

    private SsqBiz biz = new SsqBiz();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        String startdate = request.getParameter("startdate");
//        String endsdate = request.getParameter("endsdate");
//        System.out.println(startdate);
//        System.out.println(endsdate);
////        List<Map<String,Object>> list = biz.query(startdate,endsdate);
////        if( list == null || list.isEmpty() ) { // 查无数据
////            this.send(response, 500, null);
////            return;
////        }
////        this.send(response, 200, list);
//        request.setCharacterEncoding("utf-8");
//        response.setCharacterEncoding("utf-8");
//        response.setContentType("text/html;charset=utf-8");
//        Gson gson = new Gson();
//        String json = gson.toJson(biz.query(startdate,endsdate));
//        response.getWriter().print(json);
    }

    protected void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String startdate = request.getParameter("startdate");
        String endsdate = request.getParameter("endsdate");
        System.out.println(startdate);
        System.out.println(endsdate);
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
//        List<Map<String,Object>> list = biz.query(startdate,endsdate);
        SqlSession session = MyBatisHelper.getsSession();
        SsqMapper mapper = session.getMapper(SsqMapper.class);
//        List<Map<String,Object>> list = mapper.selectSQL("select * from ssq_lottery where opendate BETWEEN startdate and endsdate order by id desc limit 0, 10");
        List<Ssq_lottery> list = mapper.query(startdate,endsdate);
        if( list == null || list.isEmpty() ) { // 查无数据
            this.send(response, 500, null);
            return;
        }
//        int total = 10;
//        this.send(response,total);
        this.send(response, 200, list);


//        Gson gson = new Gson();
//        String json = gson.toJson(biz.query(startdate,endsdate));
//        response.getWriter().print(json);
    }

    protected void findByFirst(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int page = Integer.parseInt(request.getParameter("page"));
        int rows = Integer.parseInt(request.getParameter("rows"));
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        SqlSession session = MyBatisHelper.getsSession();
        SsqMapper mapper = session.getMapper(SsqMapper.class);
        Map<String, Object> map = new HashMap<String, Object>();
//        map.put("total", biz.total()); // 总记录数
//        map.put("rows", biz.findByPage(page, rows)); // 每页显示的数据
        map.put("total", mapper.total()); // 总记录数
//        map.put("rows", mapper.selectSQL("select * from ssq_lottery  order by id desc limit (page-1)*rows,rows")); // 每页显示的数据
        map.put("rows", mapper.findByPage((page-1)*rows, rows)); // 每页显示的数据
        if (map == null || map.isEmpty()) { // 查无数据
            this.send(response, 500, null);
            return;
        }
        this.send(response, 200, map);

    }

    // 点击分页码查询 findByPage
    protected void findByPage(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        int page = Integer.parseInt( request.getParameter("page"));
        int rows = Integer.parseInt( request.getParameter("rows"));
        SqlSession session = MyBatisHelper.getsSession();
        SsqMapper mapper = session.getMapper(SsqMapper.class);
        this.send(response, mapper.findByPage((page-1)*rows, rows));
//        this.send(response, biz.findByPage(page, rows));
//        this.send(response, mapper.selectSQL("select * from ssq_lottery  order by id desc limit (page-1)*rows,rows"));
    }

}
